<?php
/*
*操作动作模块
*动作对应到操作类型
*/

class Action_model extends CI_Model{

	public function __construct(){
		$this->load->database();
	}
	
	//添加动作
	public function addAction($data){

		return $this->db->insert('admin_action',$data);
	}
	
	//获取动作信息
	public function getAction($slug = false){
		if($slug === false){
			//get all action
			$query = $this->db->get('admin_action');
			return $query->result_array();
		}
		//get one action
		$id = $slug;
		$where = array(
			'id' => $id
		);
		$query = $this->db->get_where('admin_action',$where);
		return $query->row_array();
		
	}
	
	//动作列表，创建树使用
	public function getAllAction(){
		
		$actions = array();
		$this->db->order_by('id','asc');
		if($action = $this->db->get('admin_action')){
			foreach($action->result_array() as $action){
				//创建数组第一维，存入所有节点的全部信息
				
				$actions[$action['id']] = $action;
			}
				
			$temp = array();
				
			//通过遍历数组
			foreach($actions as $id => $action){
		
				//当遇到子节点
				if($action['parent_id'] >0){
						
					//创建相应父节点
					$actions[$action['parent_id']]['children'][$action['id']] = &$actions[$action['id']];
					$temp[] = $id;
		
				}
			}
				
			foreach($temp as $k => $t){
			
				unset($actions[$t]);
			}
		}
		
		return $actions;
	} 
	
	//获取action分组
	public function getTopAction(){
		$this->db->where('parent_id = 0');
		$query = $this->db->get('admin_action');
		return $query->result_array();
	}
	
	//action 是否已经存在
	/*
	 * $action_code action的标示
	 * */
	public function haveAction($action_code){
		$query = $this->db->get_where('admin_action',array('action_code'=>$action_code));
		return count($query->row_array());
	}
	
	//更新动作
	public function editAction(){
		$data = array(
			'action_code' => $this->input->post('action_code'),
			'action_info' => $this->input->post('action_info'),
			'parent_id' => $this->input->post('parent_id')
		);
		$where = array(
			"id" => $this->input->post('id')
		);
		$this->db->update('admin_action',$data,$where);
		
		return ($this->db->affected_rows()==1) ? true : false;
	} 
	
	//删除动作
	public function deleteAction(){
	
	}

}